package Command;

import java.io.File;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import Control.ICommandExecutor;

public class Dir implements ICommandExecutor {
	private Dir(){
		PropertyConfigurator.configure("log4j.properties");
	}
	private static Dir instance;
	public static ICommandExecutor getInstance()
	{
		if (instance == null)
		{
			instance = new Dir();
		}
		return instance;
	}
	public static final Logger log = Logger.getLogger(Dir.class);
	public void execute(String command)
	{
		String[] cmd = command.split(" ");
		getDir(cmd[1]);
	}
	
	public void getDir(String patch)
	{
		if (new File(patch).exists())
		{
			log.info("Log");
			File dir = new File(patch);
			File[] contetDir = dir.listFiles();
			for(int i=0;i<contetDir.length;i++)
			{
				System.out.print(contetDir[i].getName());
				if (contetDir[i].isDirectory())
				{
					System.out.print(" <DIR>");
				}
				else
				{
					System.out.print(" <FILE>, last modified date " +contetDir[i].lastModified()+" , size "+contetDir[i].length()+" byte");
				}
				System.out.println();
			}
		}
		else
		{
			log.debug("Thit patch "+patch+" is missing");
		}
	}
}
